<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<link rel="stylesheet" type="text/css"
			href="<%=basePath%>common/ui/jquery-easyui-1.3.0/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css"
			href="<%=basePath%>common/ui/jquery-easyui-1.3.0/themes/icon.css">
		<script type="text/javascript"
			src="<%=basePath%>common/ui/jquery-easyui-1.3.0/jquery-1.7.2.min.js"></script>
		<script type="text/javascript"
			src="<%=basePath%>common/ui/jquery-easyui-1.3.0/jquery.easyui.min.js"></script>
		<script type="text/javascript"
			src="<%=basePath%>common/ui/jquery-easyui-1.3.0/locale/easyui-lang-zh_CN.js"></script>
			
		<link rel="stylesheet" href="<%=basePath%>common/kindeditor-4.1.7/themes/default/default.css" />
		<script src="<%=basePath%>common/kindeditor-4.1.7/kindeditor.js"></script>
		<script src="<%=basePath%>common/kindeditor-4.1.7/lang/zh_CN.js"></script>
<script>

KindEditor.ready(function(K) {
	var editor = K.editor({
		cssPath : '<%=basePath%>common/kindeditor-4.1.7/plugins/code/prettify.css',
		uploadJson : '<%=basePath%>common/kindeditor-4.1.7/jsp/upload_json.jsp',
		fileManagerJson : '<%=basePath%>common/kindeditor-4.1.7/jsp/file_manager_json.jsp',
		allowFileManager : true,
	});
	K('#image1').click(function() {
		editor.loadPlugin('image', function() {
			editor.plugin.imageDialog({
				imageUrl : K('#imageUrl').val(),
				clickFn : function(url, title, width, height, border, align) {
					K('#imageUrl').val(url);
					var localImagId = document.getElementById("imageView");
                    localImagId.src=url;
					editor.hideDialog();
				}
			});
		});
	});
});


var grid;
var dlg_Edit;
var dlg_Edit_form;

$(function(){
	
	dlg_Edit = $('#Dlg-Edit').dialog({
        closed: true,
        modal: true,
        toolbar: [{
            text: '保存',
            iconCls: 'icon-save',
            handler: saveData
        }, '-', {
            text: '关闭',
            iconCls: 'icon-no',
            handler: function () {
                dlg_Edit.dialog('close');
            }
        }]
    });
    
    dlg_Edit_form = dlg_Edit.find('form'); 
	
	
	grid =$('#tt').datagrid({
        title:'',
        iconCls:'icon-ok',
        nowrap: true,
		autoRowHeight: true,
		striped: true,
        pageList:[15,20,30],
        striped: true,
        border:false,
        fit:true,
		fitColumns:true,
		pagination:true,
		rownumbers:true,
		idField:'beanId',
        collapsible:true,
        url:'<%=basePath%>manage/admin/product!dataList.action?productType=${productType}',
        singleSelect:false,
        frozenColumns:[[
		                {field:'ck',checkbox:true},
		                {title:'code',field:'beanId',width:40,sortable:true,align:'center',}
					]],
        columns:[[
            {field:'imageUrl',  title:'产品图片',width:50, align:'center',
            	formatter:function(value,row,index){
                 return '<img src="'+row.imageUrl+'" width="50px" height="50px"/>';
                  }            	
                },
            {field:'infoTitle',title:'产品名称',width:50, align:'center'},
            {field:'infoPrice',title:'产品价格',width:50, align:'center'},
            {field:'infoSource',title:'产品来源',width:50, align:'center'},
            {field:'absoluteUrl',title:'产品路径',width:50, align:'center'},
            {field:'email',title:'邮箱',width:120,align:'center'},
            {field:'operator',title:'操作列',width:70,rowspan:2,align:'center',
                formatter:function(value,row,index){
            	    var a = '<a href=<%=basePath%>manage/admin/product!edit.action?productType=${productType}&optionId='+row.beanId+' style="text-decoration: none;color: #800080;">编辑</a> ';
                    var e = '<a href="javascript:void(0);" onclick="editRow('+row.beanId+')" style="text-decoration: none;color: #800080;">编辑</a> ';
                    var d = '<a href="javascript:void(0);" onclick="deleteRow('+row.beanId+')" style="text-decoration: none;color: #800080;">删除</a>';
                    return a+e+d;
                }
            }        
        ]],
        pagination:true,
        rownumbers:true,
        toolbar:[{
             text:'增加',
             iconCls:'icon-add',
             handler:add
          },'-',{
              text:'查询',
              iconCls:'icon-search',
              handler:function(){
                  $('#query').window('open');
              }
          }, '-', {
              text: '删除',
              iconCls: 'icon-remove',
              handler: deleteSelectedsRow
          }
        ],
        onBeforeEdit:function(index,row){
            row.editing = true;
            $('#tt').datagrid('refreshRow',index);
            count++;
        },
        onAfterEdit:function(index,row){
            row.editing = true;
            $('#tt').datagrid('refreshRow',index);
            count--;
        },
        onCancelEdit:function(index,row){
            row.editing = false;
            $('#tt').datagrid('refreshRow',index);
            count--;
        }
    });
});
    var count = 0;
    function getSelectedArr() {
        var ids = [];
        var rows =$('#tt').datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].beanId);
        }
        return ids;
    }
    function arr2str(arr) {
        return arr.join(',');
    }
    
    function deleteRow(row){
    	var ids = [];
    	ids[0]=row;
    	deleteSelecte(ids);
    }
    function deleteSelectedsRow(){
    	var arr = getSelectedArr();
    	deleteSelecte(arr);
      }
    function deleteSelecte(arr){
        
        if (arr.length > 0) {
            $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
                if (data) {
                    $.ajax({
                        url:'<%=basePath%>manage/admin/product!remove.action?productType=${productType}&ids='+ arr2str(arr),
                        type: 'GET',
                        timeout: 1000,
                        error: function () {
                            $.messager.alert('错误', '删除失败!', 'error');
                        },
                        success: function (data) {
                            eval('data=' + data);
                            if (data.success) {
                            	$('#tt').datagrid('reload');
                            } else {
                                $.messager.alert('错误', data.msg, 'error');
                            }
                        }
                    });
                }
            });
        } else {
            Msgshow('请先选择要删除的记录。');
        }
    }
    function saveRow(index){
        $('#tt').datagrid('endEdit',index);
        /*
		 * 将数据保存到数据库
		 */
        var select = $('#tt').datagrid('getSelected');
        var arr =new Array(); // 将信息保存在数组中
        arr[0]= select.id;
        arr[1]= select.name;
        arr[2]= select.email;
        arr[3]= select.qq;
        arr[4]= select.birthday;
        arr[5]=select.address;
        if(select){
            $.ajax({
            type:'POST',
            url:'queryId.action',
            data:'id='+select.id,
            success:function(data){
                if(data=='0'){
                    $.messager.alert('error','学号'+select.id+'已存在,请重新编辑','error',function(){
                        $('#tt').datagrid('beginEdit', index);
                    });
                }else{
                    save(arr,index);
                }
            }
        });
        }
    }
    
    function save(arr,index){
        $.ajax({
            type:'POST',
            url:'saveInfo.action',
            data:'arr='+arr,
            success:function(data){
                if(data){
                    $.messager.alert('warning',data,'warning',function(){
                        $('#tt').datagrid('beginEdit', index);
                    })
                }else{
                    $('#tt').datagrid('reload');
                }
            }
        });
    }
    function query(){
        var queryParams = $('#tt').datagrid('options').queryParams;
        queryParams.queryWord = $('#qq').val();
        queryParams.queryType = $('#ss').val();
        $('#tt').datagrid({
            url:'queryInfo.action'
        });
        $('#query').window('close');
    }
    
    
    
    function add() {
        dlg_Edit.dialog('open');
        dlg_Edit_form.form('clear');
        dlg_Edit_form.url = '<%=basePath%>manage/admin/product!preservation.action?productType=${productType}';
    }
    function saveData() {
        dlg_Edit_form.form('submit', {
            url: dlg_Edit_form.url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (data) {
                //alert(data);
                eval('data=' + data);
                if (data.success) {
                    dlg_Edit.dialog('close');
                    grid.datagrid('reload');
                    grid.datagrid('clearSelections');
                } else {
                    $.messager.alert('错误', data.msg, 'error');
                }
            }
        });
    }

    function editRow(infoid){
    	grid.datagrid('clearSelections');  
        if (infoid){  
        	dlg_Edit.dialog('open'); 
        	dlg_Edit_form.form('load', '<%=basePath%>manage/admin/product!editPage.action?productType=${productType}&optionId='+infoid);  
            dlg_Edit_form.url = '<%=basePath%>manage/admin/product!preservation.action?productType=${productType}';
        } else {  
        	$.messager.alert('错误', "请先选择用户资料", 'error');
        }  
    } 
</script>
	</head>

	<body>
	<body class="easyui-layout">
		<div data-options="region:'center',title:'产品信息列表'" >
		<table id="tt">
		</table>
		</div>
	</div>	
		<div id="query" class="easyui-window" title="查询"
			style="padding: 10px; width: 360px; height: 100;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false">
			<div>
				<table>
					<tr>
						<td>
							<select name="select" id="ss">
								<option value="id">
									学号
								</option>
								<option value="name">
									姓名
								</option>
								<option value="qq">
									QQ
								</option>
							</select>
						</td>
						<td>
							<input type="text" name="id" id="qq" required="true">
						</td>
						<td>
							<a class="easyui-linkbutton" iconCls="icon-search"
								href="javascript:void(0);" onclick="query()">查询</a>
						</td>
					</tr>
				</table>
			</div>
		</div>
		
		<div id="Dlg-Edit" title="添加窗口" style="width:500px; height: 500px;">
			<div style="padding: 20px 20px 20px 20px;">
				<form method="post">
					  <input type="hidden" name="beanId"/>
					  <input type="hidden" name="imageUrl" id="imageUrl"/>
					  <div >
	                     <p><label >图片浏览:</label>
	                     <img src="" name="imageView" id="imageView" width="100px" height="100px"/><input type="button" id="image1"  value="选择图片" /></p>
	                  </div>
	                  <div>
	                     <label >产品路径:</label>
	                     <input class="easyui-validatebox" type="text" name="absoluteUrl" data-options="required:true" style="width: 300px;" />
	                  </div>
	                  <div>
	                     <label >产品名称:</label>
	                     <input class="easyui-validatebox" type="text" name="infoTitle" data-options="required:true" style="width: 300px;"/>
	                  </div>
	                  <div>
	                     <label >产品价格:</label>
	                     <input class="easyui-validatebox" type="text" name="infoPrice" data-options="required:true" style="width: 300px;"/>
	                  </div>
	                  <div>
	                     <label >产品来源:</label>
	                     <input class="easyui-validatebox" type="text" name="infoSource" data-options="required:true" style="width: 300px;"/>
	                  </div>
	                  <div>
	                     <label >所属分类:</label>
	                    <select class="easyui-combotree" url="<%=basePath%>manage/admin/product_categories!dataList.action"
									name="categorie" style="width: 300px;" />
	                  </div>
	                  
				</form>
			</div>
		</div>
	</body>
</html>
